package commands;

import java.util.Date;

import logic.Parser;
import logic.UndoRedoManager;
import storage.Task;
import storage.TaskMemoryController;
import storage.TaskPriority;
import storage.TaskStatus;
import storage.UndoRedoDetails;

public class Edit {

	public String editTask(int taskId, String field, String newValue) {
		String feedback = "";
		feedback = editTaskByField(taskId, field, newValue);
		return feedback;
	}

	public String editTaskByField(int id, String editingField, Object newValue) {
		Search search = new Search();
		Task task = TaskMemoryController.getTaskById(id);

		// title
		if (editingField.equals("title")) {
			UndoRedoManager undoRedoManager = new UndoRedoManager();
			if (undoRedoManager.isNewCommand()) {
				UndoRedoDetails undoDetail_ = new UndoRedoDetails();
				undoDetail_.setDetails("edit", task.getId(), null, "title",
						task.getTitle(), newValue);
				undoRedoManager.storeChanges(undoDetail_);
			}

			task.setTitle((String) newValue);
			TaskMemoryController.setTask(task, id);

			return "Title changed to " + task.getTitle() + "\n";
			// deadline
		} else if (editingField.equals("deadline")) {
			Parser p = new Parser((String) newValue);
			if (!p.isInAcceptableFormat())
				return "Invalid Date format. No changes made." + newValue;

			String initialValue = getMileStone(task.getDeadline(), task.getAmount(),
					task.getInterval());
			String editedValue = getMileStone(p.getDate(),
					p.getNumberOfOccurrences(), p.getPeriod());
			System.out.println("Initial Value:" + initialValue);
			System.out.println("New Value:" + editedValue);
			System.out.println("Initial Value:" + new Parser(initialValue).getDate());
			System.out.println("New Value:" + new Parser(editedValue).getDate());

			task.setDeadline(p.getDate());
			task.setAmount(p.getNumberOfOccurrences());
			task.setInterval(p.getPeriod());
			TaskMemoryController.setTask(task, id);

			UndoRedoManager undoRedoManager = new UndoRedoManager();
			if (undoRedoManager.isNewCommand()) {
				UndoRedoDetails undoDetail_ = new UndoRedoDetails();
				undoDetail_.setDetails("edit", task.getId(), null, "deadline",
						initialValue, editedValue);
				undoRedoManager.storeChanges(undoDetail_);
			}

			return "Deadline changed to " + p.getDate().toString() + "\n";

			// status
		} else if (editingField.equals("status")) {
			String initialStatus = "";
			if (task.getStatus().toString().equals("Complete")) {
				initialStatus = "1";
			} else if (task.getStatus().toString().equals("Incomplete")) {
				initialStatus = "0";
			}

			if (newValue.equals("0")) {
				UndoRedoManager undoRedoManager = new UndoRedoManager();
				if (undoRedoManager.isNewCommand()) {
					UndoRedoDetails undoDetail_ = new UndoRedoDetails();
					undoDetail_.setDetails("edit", task.getId(), null, "status",
							initialStatus, newValue);

					undoRedoManager.storeChanges(undoDetail_);
				}
				task.setStatus(TaskStatus.INCOMPLETE);
				TaskMemoryController.setTask(task, id);

				return "Status changed to Incomplete" + "\n";
			} else if (newValue.equals("1")) {
				UndoRedoManager undoRedoManager = new UndoRedoManager();
				if (undoRedoManager.isNewCommand()) {
					UndoRedoDetails undoDetail_ = new UndoRedoDetails();
					undoDetail_.setDetails("edit", task.getId(), null, "status",
							initialStatus, newValue);
					undoRedoManager.storeChanges(undoDetail_);
				}
				task.setStatus(TaskStatus.COMPLETE);
				TaskMemoryController.setTask(task, id);
				return "Status changed to Complete" + "\n";
			} else {
				return "Invalid choice. No changes made.";
			}
			// priority
		} else if (editingField.equals("priority")) {
			String initialPriority = "";
			if (task.getPriority().toString().equals("Urgent")) {
				initialPriority = "0";
			} else if (task.getPriority().toString().equals("Keep in mind")) {
				initialPriority = "1";
			} else if (task.getPriority().toString().equals("Don't bother")) {
				initialPriority = "2";
			}

			if (newValue.equals("0")) {
				UndoRedoManager undoRedoManager = new UndoRedoManager();
				if (undoRedoManager.isNewCommand()) {
					UndoRedoDetails undoDetail_ = new UndoRedoDetails();
					undoDetail_.setDetails("edit", task.getId(), null, "priority",
							initialPriority, newValue);
					undoRedoManager.storeChanges(undoDetail_);
				}

				task.setPriority(TaskPriority.HIGH);
				TaskMemoryController.setTask(task, id);
				// feedback = editTaskByField(task.getId(), "priority", entry_);
				return "Status changed to Urgent" + "\n";
			} else if (newValue.equals("1")) {
				UndoRedoManager undoRedoManager = new UndoRedoManager();
				if (undoRedoManager.isNewCommand()) {
					UndoRedoDetails undoDetail_ = new UndoRedoDetails();
					undoDetail_.setDetails("edit", task.getId(), null, "priority",
							initialPriority, newValue);
					undoRedoManager.storeChanges(undoDetail_);
				}
				task.setPriority(TaskPriority.MEDIUM);
				TaskMemoryController.setTask(task, id);

				return "Status changed to Keep in mind" + "\n";
			} else if (newValue.equals("2")) {
				UndoRedoManager undoRedoManager = new UndoRedoManager();
				if (undoRedoManager.isNewCommand()) {
					UndoRedoDetails undoDetail_ = new UndoRedoDetails();
					undoDetail_.setDetails("edit", task.getId(), null, "priority",
							initialPriority, newValue);
					undoRedoManager.storeChanges(undoDetail_);
				}
				task.setPriority(TaskPriority.LOW);
				TaskMemoryController.setTask(task, id);

				return "Status changed to Don't bother" + "\n";
			} else {
				System.out.println("Invalid choice. No changes made.");
				return "";
			}
		}
		System.out.println("No changes made.");
		return "";
	}

	private String getMileStone(Date start, int amount, int interval) {
		String result = "2000 first of Jan 0:00am ";
		long x = start.getTime() / 1000, y = new Parser(result).getDate().getTime() / 1000;
		long diff = x - y;
		if (diff >= 0)
			result = diff + " seconds after " + result;
		else
			result = (-diff) + " seconds before " + result;
		return result + " every " + interval + " seconds repeat " + amount;
	}
}
